package org.hxzon.cyclone.fgen;

import java.util.ArrayList;
import java.util.List;

import org.hxzon.cyclone.RigidBody;
//fgen.h,fgen.c
//力注册中心

public class ForceRegistry {

    /**
    * Keeps track of one force generator and the body it
    * applies to.
    */
    protected static class ForceRegistration {
        RigidBody body;
        ForceGenerator fg;
    };

    /**
    * Holds the list of registrations.
    */
    private List<ForceRegistration> registrations = new ArrayList<ForceRegistration>();

    /**
    * Registers the given force generator to apply to the
    * given body.
    */
    public void add(RigidBody body, ForceGenerator fg) {
        ForceRegistration registration = new ForceRegistration();
        registration.body = body;
        registration.fg = fg;
        //registrations.push_back(registration);
        registrations.add(registration);
    }

    /**
    * Removes the given registered pair from the registry.
    * If the pair is not registered, this method will have
    * no effect.
    */
    public void remove(RigidBody body, ForceGenerator fg) {
        ForceRegistration t = null;
        for (ForceRegistration r : registrations) {
            if (r.body == body && r.fg == fg) {
                t = r;
                break;
            }
        }
        registrations.remove(t);
    }

    /**
    * Clears all registrations from the registry. This will
    * not delete the bodies or the force generators
    * themselves, just the records of their connection.
    */
    public void clear() {
        registrations.clear();
    }

    /**
    * Calls all the force generators to update the forces of
    * their corresponding bodies.
    */
    public void updateForces(double duration) {
        for (ForceRegistration fr : registrations) {
            fr.fg.updateForce(fr.body, duration);
        }
    }
}
